package com.token.sysflow.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.token.sql.IntegrateSql;
import com.token.sysflow.core.GetPathApprover;
import com.token.sysflow.enumerate.ActionTypeEnum;
import com.token.sysflow.enumerate.ApprovalTypeEnum;
import com.token.sysflow.hander.*;
import lombok.Getter;
import lombok.Setter;

import java.util.List;
import java.util.Objects;

/**
 * 获流程的下一环节和办理人
 * 一定要有get 方法，不能无法回写参数
 */
public class GetPathApproverImpl extends AbstractFlowHandlerImpl {

    /**
     * 首次提交时需要传入:流程Id
     */
    @Setter
    @Getter
    private String flowId;

    @Setter
    @Getter
    private String stepId;

    /**
     * 首次提交时需要传入:申请人
     */
    @Setter
    @Getter
    private String applyer;

    /**
     * 首次提交时需要传入:创建人
     */
    @Setter
    @Getter
    private String creater;
    /**
     * 初始化参数
     *
     * @param iSql 类型
     * @throws Exception 抛出错误
     */
    public void initParameter(IntegrateSql iSql) throws Exception {
        if (keyUnid == null || "".equals(keyUnid)) { //首次提交时。
            flowCore = new GetPathApprover(iSql);
        } else {
            flowCore = new GetPathApprover(iSql, keyUnid);
        }
    }

    /**
     * 提交操作类型
     *
     * @return
     */
    public ActionTypeEnum getActionType() {
        return ActionTypeEnum.GETPATH;
    }

    /**
     * @throws Exception
     */
    public void handleReturnResult() throws Exception {
        Flow flow = flowCore.getFromFlow();
        Step step = flowCore.getFromStep();

        this.setReturnJson("defaultOpinion",step.getDefaultOpinion());
        this.setReturnJson("flowName",flow.getFlowName() + " " + flow.getFlowVersion());
        ApproveProcess process = flowCore.getApproveProcess();
        if(process != null) {
            this.setFlowNo(process.getFlowNo()); //用于回写提交日志。
            this.setReturnJson("approvalType",process.getApprovalType());
            ApprovalTypeEnum type = ApprovalTypeEnum.getTypeByValue(process.getApprovalType());
            this.setReturnJson("approvalTypeTitle", type.getName());
            switch (Objects.requireNonNull(type)) {
                case HS:
                case HQ:
                case ZZHQ:
                case ZZHS:
                    return;
            }
        }else{
            this.setReturnJson("approvalType",0);
            this.setReturnJson("approvalTypeTitle", "起草");
        }

        // 会审会签类，不用输出下面的代码。
        JSONObject jsonObject  = this.getFieldValues(); //传入的字段值
        if(jsonObject == null){
            jsonObject = new JSONObject();
        }
        jsonObject.putAll(flowCore.getUserInfoMap());//添加申请人、起草人、当前人信息。
        List<Step> stepList = step.getAllNextStep(jsonObject);

        JSONObject json,json2;
        JSONArray pathArray = new JSONArray();
        JSONArray approver;
        StepApprover stepApprover;

        for (Step step1 : stepList) {
            stepApprover = new StepApprover(step1.getIntegrateSql(),step1);
            stepApprover.setProcess(process); //process 允许null
            stepApprover.setFlowHandlerImpl(this);
            stepApprover.setApplyer(this.applyer); //申请用户
            stepApprover.setCreater(this.creater); //起草用户
            stepApprover.execute();

            json = new JSONObject();
            approver = stepApprover.getApprovers();

            json.put("approver", approver);
            json.put("stepId", step1.getStepId());
            json.put("disable",step1.isSelectWayCheck());
            json.put("stepName", step1.getStepName());
            json.put("selectWay", step1.getSelectWay());
            json.put("flowId", flow.getFlowId());
            json.put("isReturned",0);
            for(int i=0;i < approver.size();i++){
                json2 = approver.getJSONObject(i);
                json2.put("checked",step1.isNoChecked());
            }
            pathArray.add(json);
            stepApprover.close();
        }

        if(process != null) {
            if (process.isReturned()) {
//                ApprovalTypeEnum type = ApprovalTypeEnum.getTypeByValue(process.getApprovalType());
//
//                switch (Objects.requireNonNull(type)) {
//                    case HS:
//                    case HQ:
//                    case ZZHQ:
//                    case ZZHS:
//                        return;
//                }
                if(step.isCanSubmitRePerson()) { //允许提交退回人
                    json = new JSONObject();
                    Step step2 = new Step(step.getIntegrateSql(), process.getPreStepId());
                    json.put("approver", Person.getUserData(flowCore.getISql(),process.getPrevApprover(), this.getSysId()));
                    json.put("stepId", step2.getStepId());
                    json.put("disable", step2.isSelectWayCheck());
                    json.put("stepName", "直接提交退回人[" + step2.getStepName() + "]");
                    json.put("selectWay", step2.getSelectWay());
                    json.put("flowId", flow.getFlowId());
                    json.put("checked", true);
                    json.put("isReturned", 1);
                    pathArray.add(json);
                }
            }
        }
        this.setReturnJson("data",pathArray);
    }
}